<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用一个行列布局 -->
        <tree-tool :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次，有多少个节点就循环多少次 -->
          <!-- 作用域插槽 slot-scope =“obj” 接收传递给插槽的数据 data每个节点的数据对象 -->
          <tree-tool slot-scope="{ data }" :tree-node="data" @addDepts="addDepts" @delDepts="getDepartments" @editDepts="editDepts" />
        </el-tree>
      </el-card>
      <!-- 新增部门弹层 -->
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import TreeTool from './components/tree-tools.vue'
import addDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    TreeTool, addDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name', // 表示从这个属性显示内容
        children: 'children'
      },
      // 这是头部的数据结构
      company: { },
      showDialog: false, // 控制显示添加弹层的
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取数据
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '') // 需要将其转化成树形结构
      console.log(result)
    },
    addDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node // 穿过来的node 是当前点击需要添加子部门的节点。需要记录下来
    },
    editDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node // 传过来的点击编辑的节点
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
